package com.sskj.fca.ui.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.fca.R;
import com.sskj.fca.bean.BankBean;
import com.sskj.fca.bean.ShowPayBean;
import com.sskj.fca.presenter.AddPayWayPresenter;
import com.sskj.fca.ui.ArrowAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * Created by Q-Kang on 2019/1/20.
 */

@Route(path = RConfig.APP_FRAGMENT_PAY_WAY)
public class AddPayWayFragment extends BaseFragment<AddPayWayPresenter> {
    @BindView(R.id.et_alipay)
    EditText etAlipay;
    @BindView(R.id.et_real_name)
    EditText etRealName;
    @BindView(R.id.et_idcard)
    EditText etIdcard;
    @BindView(R.id.et_opening_bank)
    EditText etOpeningBank;
    @BindView(R.id.tv_bankeName)
    TextView tv_bankeName;
    @BindView(R.id.et_usdt_address)
    EditText et_usdt_address;
    @BindView(R.id.spinner_bank)
    Spinner spinner_bank;
    @BindView(R.id.et_opening_branch_bank)
    EditText et_opening_branch_bank;
    @BindView(R.id.btn_save_pay_way)
    Button btnSavePayWay;

    private String bankName = "";
    private String bankCode = "";
    private boolean isFirstChoose = true;

    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_pay_way;
    }

    @Override
    protected AddPayWayPresenter getPresenter() {
        return new AddPayWayPresenter();
    }

    @Override
    public void initView() {
        super.initView();
        mPresenter.showPay();
        ClickUtil.click(btnSavePayWay, () -> {
            if (TextUtils.isEmpty(etRealName.getText().toString().trim())) {
                ToastUtil.showShort("请输入您的真实姓名");
                return;
            }
            if (TextUtils.isEmpty(etIdcard.getText().toString().trim())) {
                ToastUtil.showShort("请输入您的银行卡号");
                return;
            }
            if (TextUtils.isEmpty(tv_bankeName.getText().toString())) {
                ToastUtil.showShort("请选择您的银行卡开户行");
                return;
            }
            if (TextUtils.isEmpty(bankCode)) {
                ToastUtil.showShort("请选择您的银行卡开户行");
                return;
            }
            if (TextUtils.isEmpty(et_opening_branch_bank.getText().toString().trim())) {
                ToastUtil.showShort("请输入您的银行卡开户行支行");
                return;
            }
//            if (TextUtils.isEmpty(et_usdt_address.getText().toString().trim())) {
//                ToastUtil.showShort("请输入钱包地址");
//                return;
//            }
            mPresenter.addPayWay(et_usdt_address.getText().toString().trim()
                    , etRealName.getText().toString().trim()
                    , etIdcard.getText().toString().trim()
                    , tv_bankeName.getText().toString()
                    , bankCode
                    , et_opening_branch_bank.getText().toString().trim());
        });
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter.getBanks();
    }

    public void showPayWay(ShowPayBean showPayBean) {
        etRealName.setText(showPayBean.getRealname());
        etIdcard.setText(showPayBean.getBankcard());
        tv_bankeName.setText(showPayBean.getBranch());
        et_opening_branch_bank.setText(showPayBean.getBank());
        et_usdt_address.setText(showPayBean.getAddress());
    }

    public void getBanksOK(List<BankBean> data) {
        if (data == null) {
            return;
        }
        Observable.fromArray(data)
                .map(bankerHistoryBeans -> {
                    List<String> strings = new ArrayList<>();
                    for (int i = 0; i < bankerHistoryBeans.size(); i++) {
                        strings.add(bankerHistoryBeans.get(i).getBank_name());
                    }
                    return strings;
                }).compose(RxSchedulersHelper.io_main())
                .subscribe(strings -> {
                    ArrowAdapter spinnerWayAdapter = new ArrowAdapter(getActivity(),
                            R.layout.app_item_user_type, strings);
                    spinnerWayAdapter.setDropDownViewResource(R.layout.app_item_withdraw_type_drop);
                    spinner_bank.setAdapter(spinnerWayAdapter);
                    if (strings.contains(tv_bankeName.getText().toString())) {
                        spinner_bank.setSelection(strings.indexOf(tv_bankeName.getText().toString()));
                    } else {
                        spinner_bank.setSelection(0);
                    }
                    spinner_bank.setDropDownVerticalOffset(10);
                    spinner_bank.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            if (isFirstChoose) {
                                if (strings.contains(tv_bankeName.getText().toString())) {
                                    bankCode = data.get(position).getBank_code();
                                    tv_bankeName.setText(data.get(position).getBank_name());
                                }
                                isFirstChoose = false;
                            } else {
                                bankCode = data.get(position).getBank_code();
                                tv_bankeName.setText(data.get(position).getBank_name());
                            }
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    });
                });

    }
}
